#include "log.h"
#include "usart.h"
#include "user_shell.h"
#include "SEGGER_RTT.h"
#include "tool.h"

void log_write(char *buff,short len);

Shell shell;

char shellBuffer[512];
Log uartLog = {
    .write = log_write,
    .active = LOG_ENABLE,
    .level = LOG_DEBUG
};
    
signed short shell_read(char *buff, unsigned short len) 
{
	if(SEGGER_RTT_HasKey())
	{
		uint8_t ucGetChar;
		ucGetChar = SEGGER_RTT_GetKey();
		buff[0] = ucGetChar;
		return 1;
	}
	
	return 0;

}

signed short shell_write(char *buff, unsigned short len)
{
	SEGGER_OUT(buff, len);
  return len;
}

void log_write(char *buff,short len)
{
	SEGGER_OUT(buff, len);
}

void userShellInit(void)
{
    shell.write = shell_write;
    shell.read = shell_read;
    shellInit(&shell, shellBuffer, 512);
    logRegister(&uartLog, &shell);
}


